package com.radar.license;

import cn.hutool.core.io.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Configuration
public class LicenseConfig {

//    /**
//     * 证书subject
//     */
//    @Value("${license.subject}")
//    private String subject;
//
//    /**
//     * 公钥别称
//     */
//    @Value("${license.publicAlias}")
//    private String publicAlias;
//
//    /**
//     * 访问公钥库的密码
//     */
//    @Value("${license.storePass}")
//    private String storePass;

    /**
     * 证书生成路径
     */
    @Value("${licensePath}")
    private String licensePath;

//    /**
//     * 密钥库存储路径
//     */
//    @Value("${license.publicKeysStorePath}")
//    private String publicKeysStorePath;

    @Bean(initMethod = "installLicense", destroyMethod = "unInstallLicense")
    public LicenseVerify licenseVerify() {

        try (InputStream provinceStream = LicenseConfig.class.getClassLoader().getResourceAsStream("radarflow_public_certs.store")) {
            FileUtil.writeFromStream(provinceStream, System.getProperty("java.io.tmpdir") + File.separator + "radarflow_public_certs.store");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new LicenseVerify("licenseTest", "radarflow_publiccert", "Zhongz62", licensePath, System.getProperty("java.io.tmpdir") + File.separator + "radarflow_public_certs.store");
    }

}
